﻿namespace DesktopRibbon
{
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Media;
    using System.Windows.Input;

    /// <summary>
    /// This is the Desktop Ribbon main window, which hosts the DesktopRibbonControl
    /// </summary>
    public partial class DesktopRibbonWindow : Window
    {
        #region Fields

        private Brush inactiveBackgroundBrush = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255));
        private ThirdParty.KeyboardHook toggleHotkey;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes the DesktopRibbonWindow with it's default values.
        /// Initially, it will be collapsed (IsCollapsed = true).
        /// </summary>
        public DesktopRibbonWindow()
        {
            InitializeComponent();
            IsCollapsed = true;
            Screen s = Screen.AllScreens [0];
            Left = s.Bounds.Left;
            Top = s.Bounds.Top;
            Width = s.Bounds.Width;

            toggleHotkey = RegisterActivationHotkey(Keys.Q);

            KeyDown += new System.Windows.Input.KeyEventHandler(HandleKeyDown);
        }

        /// <summary>
        /// Cleans up the DesktopRibbonWindow's member (currently unregisters the hotkeys).
        /// </summary>
        ~DesktopRibbonWindow()
        {
            toggleHotkey.Dispose();
        }
        
        #endregion

        #region Properties

        /// <summary>
        /// IsCollapsed is true when the ribbon is only a few pixels high and displayed with only a little opacity.
        /// IsCollapsed is true, when the ribbon is fully visible.
        /// </summary>
        public bool IsCollapsed {
            get {
                return Height == 2;
            }
            set {
                if (value != IsCollapsed)
                {
                
                    if (value)
                    {
                        Height = 2;
                        Background = inactiveBackgroundBrush;
                    } else {
                        Height = 250;
                        Background = Brushes.Transparent;
                    }
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Sets <see cref="IsCollapsed">IsCollapsed</see>.
        /// </summary>
        /// <param name="collapsed">The value that IsCollapsed will be set to.</param>
        public void Collapse(bool collapsed = true)
        {
            IsCollapsed = collapsed;
        }
        
        /// <summary>
        /// Sets <see cref="IsCollapsed">IsCollapsed</see>.
        /// </summary>
        /// <param name="uncollapsed">The inverted value that IsCollapsed will be set to.</param>
        public void Uncollapse(bool uncollapsed = true)
        {
            IsCollapsed = !uncollapsed;
        }

        private void Window_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Uncollapse();
        }

        private void Window_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Collapse();
        }

        private ThirdParty.KeyboardHook RegisterActivationHotkey(Keys key)
        {
            var hook = new ThirdParty.KeyboardHook();
            hook.KeyPressed += new System.EventHandler<ThirdParty.KeyPressedEventArgs>(ToggleCollapseFromHotkey);
            hook.RegisterHotKey(ThirdParty.ModifierKeys.Win, key);
            return hook;
        }

        private void ToggleCollapseFromHotkey(object sender, ThirdParty.KeyPressedEventArgs e)
        {
            IsCollapsed = ! IsCollapsed;
            Activate();
        }

        private void HandleKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                IsCollapsed = true;
            }
        }

        private void Window_DragEnter(object sender, System.Windows.DragEventArgs e)
        {
            // When a Drag & Drop operation starts, uncollapse the Desktop Ribbon.
            Uncollapse();
        }

        #endregion

    }
}
